package com.lad.multithread_transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TransactionalController {
    /*@Autowired
    private UserMapper userMapper;

    private static Map<Integer, List<User>> map = new HashMap<>();

    private PlatformTransactionManager platformTransactionManager;//手动事物管理
    private TransactionDefinition transactionDefinition;//事物*/
    @Autowired
    private TransactionalService transactionalService;

    @GetMapping("/testtc")
    public String test() {
        /*TransactionStatus transactionStatus = null;
        try {
            //手动开启事务
            transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);

            execution();

            //手动提交事物
            platformTransactionManager.commit(transactionStatus);
        } catch (Exception e) {
            //手动回滚事物
            platformTransactionManager.rollback(transactionStatus);
        }*/
        transactionalService.execution();
        return "ok";
    }

/*    public void execution(){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 1; i < 11; i++) {
            int finalI = i;
            executorService.execute(() -> {
                List<User> users = map.get(finalI);
                userMapper.insertUsers(users);
            });

            if (i == 10) {
                int a = 1/0;
            }
        }
    }*/

  /*  static {
        for (int i = 1; i < 11; i++) {
            List<User> users = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                User user = new User(i * 10 + j, "zs" + i + j, "000" + i + j, "admin" + i + j);
                users.add(user);
            }
            map.put(i, users);
        }

    }*/

}
